package com.canieatit.app.scanner;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.os.AsyncTask;

public class BarcodeDataRetriever extends AsyncTask<String, String	, String> {
	
	private final String API_KEY = "AIzaSyAO27nbl_qSqIQ5eHOiG5HBWdgHfLpagP4";
	private final String BASE_URL = "https://www.googleapis.com/shopping/search/v1/public/products?key="+API_KEY+"&country=AU&q=";
	
	@Override
	protected String doInBackground(String... arg) {
		StringBuilder url = new StringBuilder(BASE_URL);
	    url.append(arg[0]);
	    
	    DefaultHttpClient client = new DefaultHttpClient();

		try {    
	    	HttpGet get = new HttpGet(url.toString());
		    HttpResponse r = client.execute(get);
		    int status = r.getStatusLine().getStatusCode();
		    if (status == 200) {
		        HttpEntity e = r.getEntity();
		        String data = EntityUtils.toString(e);
		        JSONObject timeline = new JSONObject(data);
		        return timeline.toString();
		    } else {
	//	        Toast.makeText(getContext(), "error", Toast.LENGTH_SHORT);
		        return null;
		    }
		} catch (Exception e) {
			e.printStackTrace(System.out);
		}
		return null;
	}

}

